<!DOCTYPE html>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<style></style>
<div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div id="found">
        <div></div>
    </div>
    <span></span>
</div>
<div id="placeholderWrapper">
    <textarea placeholder="Placeholder text"></textarea>
</div>
<textarea placeholder="Placeholder text"></textarea>
<script>
    test(() => {
        assert_true(!!window.internals, "Tests require window.internals.");
    }, "Test for prerequisites.");

    function applyRuleAndReturnAffectedElementCount(ruleString) {
        document.body.offsetTop;
        document.styleSheets[0].insertRule(ruleString, 0);
        var recalcCount = internals.updateStyleAndReturnAffectedElementCount();
        document.styleSheets[0].removeRule();
        return recalcCount;
    }

    test(() => {
        assert_equals(applyRuleAndReturnAffectedElementCount(
            "span { background: green }"), 1,
            "Check that only the span is affected.");
    }, "A style rule with a type selector should only invalidate elements with that type.");

    test(() => {
        assert_equals(applyRuleAndReturnAffectedElementCount(
            "#notfound div { background: red }"), 0,
            "Check that none of divs are recalculated.");
    }, "A type selector scoped in an unknown id should not invalidate any elements.");

    test(() => {
        assert_equals(applyRuleAndReturnAffectedElementCount(
            "#found div { background: red }"), 1,
            "Check that only one of the divs is recalculated.");
    }, "A type selector scoped by a known id should only invalidate descendants of the element with that id.");

    test(() => {
        assert_equals(applyRuleAndReturnAffectedElementCount(
            "::-webkit-input-placeholder { background: green }"), 2,
            "Check that only custom pseudo elements are recalculated.");
    }, "A style rule with a custom pseudo element should only invalidate custom pseudo elements.");

    test(() => {
        assert_equals(applyRuleAndReturnAffectedElementCount(
            "#notfound ::-webkit-input-placeholder { background: green }"), 0,
            "Check that no elements are recalculated.");
    }, "A custom pseudo element scoped in an unknown id should not invalidate any elements.");

    test(() => {
        assert_equals(applyRuleAndReturnAffectedElementCount(
            "#placeholderWrapper ::-webkit-input-placeholder { background: green }"), 1,
            "Check that only the scoped custom pseudo element is recalculated.");
    }, "A custom pseudo element scoped in a known id should only invalidate descendants of the element with that id.");
</script>
